package com.aidansmeaton.weightmanager;

import java.io.FileOutputStream;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.aidansmeaton.weightmanager.data.DatabaseHelper;

public class DiaryPhotoActivity extends Activity {

	private static final int CAMERA_PIC_REQUEST = 1234;

	private DatabaseHelper db;
	private String date;
	private Cursor c;
	
	private String path;
	
	private ImageView photo;
	private ImageButton add;
	private ImageButton remove;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.diary_photo);

		db = DatabaseHelper.getInstance(this);
		date = getIntent().getStringExtra("Date");
		c = db.getPhotoLogs(date);
		
		photo = (ImageView) findViewById(R.id.photo);
		add = (ImageButton) findViewById(R.id.add);
		remove = (ImageButton) findViewById(R.id.remove);

	}

	@Override
	public void onResume() {
		super.onResume();
		c.requery();
		
		if (c.getCount() > 0) {
			c.moveToFirst();
			path = c.getString(DatabaseHelper.PHOTO_LOG_URI);
			String fullPath = getFilesDir().getAbsolutePath()+"/"+path;
    		photo.setImageBitmap(BitmapFactory.decodeFile(fullPath));
			photo.setVisibility(View.VISIBLE);
    		add.setVisibility(View.GONE);
    		remove.setVisibility(View.VISIBLE);
		}

		else {
			photo.setVisibility(View.GONE);
			add.setVisibility(View.VISIBLE);
    		remove.setVisibility(View.GONE);
		}

	}

	public void addClick(View v) {
		Intent cameraIntent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
		onResume();
	}
	
	public void removeClick(View v) {
		
		final Dialog dialog = new Dialog(this);
    	dialog.setContentView(R.layout.dialog_remove);
    	dialog.setTitle("Remove Photo?");
    	dialog.setCancelable(true);
    	
    	// force width to fill parent
    	LayoutParams params = dialog.getWindow().getAttributes(); 
        params.width = LayoutParams.FILL_PARENT; 
        dialog.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        
        Button cancel = (Button) dialog.findViewById(R.id.cancel);
        cancel.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	dialog.dismiss();
            }
        });    	
    
        Button remove = (Button) dialog.findViewById(R.id.remove);
        remove.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	// TODO delete photo
            	db.deletePhotoLogs(date);
            	onResume();
            	dialog.dismiss();
            }
        });
        dialog.show();
	}
	
	public void photoClick(View v) {
		
		Intent intent = new Intent(v.getContext(), PictureViewerActivity.class);
		intent.putExtra("Path", path);
    	v.getContext().startActivity(intent);
		
		/* Launch photo viewer activity
		Toast.makeText(v.getContext(), "Loading Photo", Toast.LENGTH_SHORT).show();
		final Uri uri = Uri.parse("file:///data/data/com.aidansmeaton.weightmanager/files/"+path);
    	Intent intent = new Intent();
    	intent.setAction(Intent.ACTION_VIEW);
    	intent.setDataAndType(uri, "image/jpg");
    	v.getContext().startActivity(intent); */
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == CAMERA_PIC_REQUEST) {
			try {
				Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            	String filename = "photo_log_"+System.currentTimeMillis();
        		FileOutputStream fos = openFileOutput(filename, Context.MODE_WORLD_READABLE);
        		thumbnail.compress(Bitmap.CompressFormat.JPEG, 95, fos);
        		fos.close();
        		
				photo.setImageBitmap(thumbnail);
				db.addPhotoLog(date, filename, "");
				
				onResume();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}